<?php

namespace app\common\model\mycurrency;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class OperateDistributionMoneylog extends Model
{

    //use SoftDelete;

    

    // 表名
    protected $name = 'mycurrency_operate_distribution_money_log';

    const TYPE_HOUTAITIANJIA = 1;//余额变动类型 -后台添加
    const TYPE_HOUTAIJIANSHAO = 2;//余额变动类型 -后台减少
    const TYPE_YONGHUXIAOFEI = 3;//余额变动类型 - 用户消费收益
    const TYPE_TIXIAN = 4;//余额变动类型 - 分销员提现
    const TYPE_TIXIANBOHUI = 5;//余额变动类型 - 分销员提现驳回

    const TYPE_YAJINZHIFU = 6;//余额变动类型 - 租赁订单押金扣款
    const TYPE_YAJINTUIHUEI = 7;//余额变动类型 - 租赁订单押金退回
    const TYPE_DINGDANZHIFU = 8;//余额变动类型 - 租赁订单支付

    /**
     * 处理余额变动
     * @param int $distribution_user_id 分销员ID
     * @param int $money 变动金额
     * @param  int $type 变动类型
     * @param array $data 数据
     */
    public static function moneyChange($distribution_user_id,$money,$type,$data=[])
    {
        $distribution_user = OperateDistributionUser::get($distribution_user_id);
        $before = $distribution_user['money'];//变更前余额
        $after = 0;//变更后余额
        if($type == self::TYPE_HOUTAITIANJIA ||
            $type == self::TYPE_YONGHUXIAOFEI ||
            $type == self::TYPE_TIXIANBOHUI ||
            $type == self::TYPE_YAJINTUIHUEI
        ){//后台添加  用户消费收益  提现驳回
            $after = $before + $money;
        }elseif (
            $type == self::TYPE_HOUTAIJIANSHAO ||
            $type == self::TYPE_TIXIAN ||
            $type == self::TYPE_YAJINZHIFU ||
            $type == self::TYPE_DINGDANZHIFU
        ){
            $after = $before - $money;
        }
        $distribution_user->money = $after;
        $distribution_user->save();

        $insertData = [
            'distribution_user_id' => $distribution_user['id'],
            'money' => $money,
            'before' => $before,
            'after' => $after,
            //'memo' => '',
            'createtime' => time(),
            'type' => $type
        ];
        $data = array_merge($insertData,$data);
        self::create($data);
    }

    public function getCreatetimeAttr($value){
        return $value != null ? date('Y-m-d H:i:s', $value) : '';
    }

}
